package com.yiren.backstage.judge.services.standard.impl;

import android.content.Context;

import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.MonitorCountEntity;
import com.yiren.backstage.currency.enums.ExecuteElementEnum;
import com.yiren.backstage.currency.enums.ResultStateEnum;
import com.yiren.backstage.db.dao.cache.MonitorCountCache;
import com.yiren.backstage.judge.services.standard.CheckCarStateIsAccordService;
import com.yiren.backstage.judge.services.standard.ICheckElementResultService;
import com.yiren.backstage.judge.thread.AutoJudgeThread;

import java.util.List;

/**
 * Time:2022/4/13 11:07
 * Author:xiaozan
 * Description:检测条件要素和执行要素结果
 */
public class CheckElementResultServiceImpl implements ICheckElementResultService {

    /**符合车辆状态开始时间  用于判断维持状态的起始点*/
    private long accordCarStateStartTime = 0;

    /**符合车辆状态开始距离  用于判断维持状态的起始点*/
    private float accordCarStateStartDistance = 0;

    /**符合车辆状态时的实际累计执行值*/
    private float totalAccordValue;

    private CheckCarStateIsAccordService checkCarStateIsAccordService;

    private CarStateBean realCarState;

    public CheckElementResultServiceImpl(Context context){
        realCarState = CarStateBean.getInstance();
        checkCarStateIsAccordService = new CheckCarStateIsAccordService(context,null);
    }

    @Override
    public boolean accordConditionElement(AutoJudgeThread autoJudgeThread, JudgeDetailsEntity judgeDetailsEntity, CarStateBean startCarState, long mStartTime){
        boolean accordConditionElement = false;
        float consumingConditionValue = 0;
        float conditionValue = judgeDetailsEntity.getConditionValue();
        //条件要素
        switch (ExecuteElementEnum.valueOf(judgeDetailsEntity.getConditionElement())) {
            case TIME:       //时间
                consumingConditionValue = (System.currentTimeMillis() - mStartTime) / 1000;
                accordConditionElement = (consumingConditionValue < conditionValue) ? true : false;
                break;
            case DISTANCE:  //距离
                consumingConditionValue = (realCarState.getTotalMileage() - startCarState.getTotalMileage()) / 1000;
                accordConditionElement = (consumingConditionValue < conditionValue) ? true : false;
                break;
            case MONITOR_STATE:   //监控状态
                List<MonitorCountEntity> monitorCountEntities = MonitorCountCache.getMonitorCounts((int)judgeDetailsEntity.getConditionValue()+"");
                if (null != monitorCountEntities) {
                    boolean accord  = checkCarStateIsAccordService.isAccordCurrentCarState(autoJudgeThread,startCarState, monitorCountEntities);
                    accordConditionElement = accord ? false : true;
                }
                break;
            default:
                break;
        }
        return accordConditionElement;
    }

    @Override
    public boolean waitAccordExecuteElement(JudgeDetailsEntity judgeDetailsEntity,boolean accordCarState){
        float executeValue = judgeDetailsEntity.getExecuteValue();
        //执行要素值是0时达到一次就符合，反之根据执行要素值符合维持状态
        if ((int) executeValue == 0) {
            return accordCarState;
        } else {
            //判断状态是否符合执行要素维持时间
            if (accordCarState) {
                switch (ExecuteElementEnum.valueOf(judgeDetailsEntity.getExecuteElement())) {
                    case TIME:
                        //获取符合状态时开始时间
                        if (accordCarStateStartTime == 0) {
                            accordCarStateStartTime = System.currentTimeMillis();
                        }
                        //维持时间
                        float keepTime = (System.currentTimeMillis() - accordCarStateStartTime) / 1000;
                        return  (keepTime >= executeValue) ? true : false;
                    case DISTANCE:
                        //获取符合状态时开始距离
                        if (accordCarStateStartDistance == 0) {
                            accordCarStateStartDistance = realCarState.getTotalMileage();
                        }
                        //维持距离
                        float keepDistance = (realCarState.getTotalMileage() - accordCarStateStartDistance) / 1000;
                        return  (keepDistance >= executeValue) ? true : false;
                }
            } else {
                //重新开始等待维持状态
                accordCarStateStartTime = 0;
                accordCarStateStartDistance = 0;
            }
        }
        return false;
    }

    @Override
    public ResultStateEnum afterWaitAccordExecuteElement(JudgeDetailsEntity judgeDetailsEntity, boolean accordCarState) {
        float executeValue = judgeDetailsEntity.getExecuteValue();
        if(accordCarState){
            if ((int) executeValue == 0) return ResultStateEnum.SUCCESS;
            switch (ExecuteElementEnum.valueOf(judgeDetailsEntity.getExecuteElement())) {
                case TIME:
                    //获取符合状态时开始时间
                    if (accordCarStateStartTime == 0) {
                        accordCarStateStartTime = System.currentTimeMillis();
                    }
                    //维持时间
                    float keepTime = (System.currentTimeMillis() - accordCarStateStartTime) / 1000;
                    return  (keepTime >= executeValue) ? ResultStateEnum.SUCCESS : ResultStateEnum.EXECUTING;
                case DISTANCE:
                    //获取符合状态时开始距离
                    if (accordCarStateStartDistance == 0) {
                        accordCarStateStartDistance = realCarState.getTotalMileage();
                    }
                    //维持距离
                    float keepDistance = (realCarState.getTotalMileage() - accordCarStateStartDistance) / 1000;
                    return  (keepDistance >= executeValue) ? ResultStateEnum.SUCCESS : ResultStateEnum.EXECUTING;
            }
        }else{
            return ResultStateEnum.FAIL;
        }
        return ResultStateEnum.EXECUTING;
    }

    @Override
    public boolean totalAccordExecuteElement(JudgeDetailsEntity judgeDetailsEntity,boolean accordCarState){
        float executeValue = judgeDetailsEntity.getExecuteValue();
        if (accordCarState) {
            switch (ExecuteElementEnum.valueOf(judgeDetailsEntity.getExecuteElement())) {
                case TIME:
                    //累计时间
                    long realTime = System.currentTimeMillis();
                    if(accordCarStateStartDistance == 0) accordCarStateStartDistance = realTime;
                    totalAccordValue += (realTime - accordCarStateStartDistance) / 1000;
                    accordCarStateStartDistance = realTime;
                    return (totalAccordValue >= executeValue) ? true : false;
                case DISTANCE:
                    //累计距离
                    float realMileage = realCarState.getTotalMileage();
                    if(accordCarStateStartDistance == 0) accordCarStateStartDistance = realMileage;
                    totalAccordValue += (realMileage - accordCarStateStartDistance) / 1000;
                    accordCarStateStartDistance = realMileage;
                    return (totalAccordValue >= executeValue) ? true : false;
            }
        } else {
            accordCarStateStartDistance = 0;
            accordCarStateStartDistance = 0;
        }
        return false;
    }
}
